//
//  InfoEditView.m
//  QuickCare
//
//  Created by lidabiao on 16/7/15.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "InfoEditView.h"
#import <Masonry/Masonry.h>

@interface InfoEditView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) UIView *containerView;

@property (weak, nonatomic) UIPickerView *pickerView;

@property (weak, nonatomic) UITextField *editTextField;

@property (weak, nonatomic) UILabel *titleLabel;

@property (weak, nonatomic) UIButton *cancelButton;

@property (weak, nonatomic) UIButton *doneButton;

@property (strong, nonatomic) UserInfo *userInfo;

@property (assign, nonatomic) EditStyle editStyle;

@property (copy, nonatomic) NSString *editTitle;

@property (strong, nonatomic) NSArray *provinceArr;

@property (copy, nonatomic) NSString *selectedProvince;

@property (assign, nonatomic) NSInteger figure;

@property (assign, nonatomic) NSInteger hundred;

@property (assign, nonatomic) NSInteger ten;

@property (assign, nonatomic) NSInteger one;

@end

@implementation InfoEditView

#pragma mark - 懒加载
-(NSArray *)provinceArr
{
    if (_provinceArr == nil)
    {
        NSArray *sourceArr =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil]];
        
        NSMutableArray *destArr = [NSMutableArray array];
        
        for (NSDictionary *dic in sourceArr)
        {
            NSString *province = dic[@"name"];
            
            [destArr addObject:province];
        }
        _provinceArr = destArr;
    }
    
    return _provinceArr;
}

#pragma mark - 设置界面
-(instancetype)initWithFrame:(CGRect)frame userInfo:(UserInfo *)userInfo editStyle:(EditStyle) editstyle title:(NSString *)title text:(NSString *)text
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.backgroundColor = [UIColor blackColor];
        
        self.alpha = 0.7;
        
        _editStyle = editstyle;
        
        _userInfo = userInfo;
        
        _editTitle = title;
        
        UIView *containerView = [[UIView alloc] init];
        
        [self addSubview:containerView];
        
        self.containerView = containerView;
        
        containerView.backgroundColor = [UIColor whiteColor];
        
        containerView.layer.cornerRadius = 5;
        
        containerView.layer.masksToBounds = YES;
        
        [self setUpUIWithTitle:title text:(NSString *)text];
    }
    return self;
}

-(void)setUpUIWithTitle:(NSString *)title text:(NSString *)text
{
    UIButton *cancelButton = [[UIButton alloc] init];
    [self addSubview:cancelButton];
    self.cancelButton = cancelButton;
    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton sizeToFit];
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *doneButton = [[UIButton alloc] init];
    [self addSubview:doneButton];
    self.doneButton = doneButton;
    [doneButton setTitleColor:[UIColor colorWithRed:47 / 256.0 green:198 / 256.0 blue:197 / 256.0 alpha:1.0] forState:UIControlStateNormal];
    [doneButton setTitle:@"确定" forState:UIControlStateNormal];
    [doneButton sizeToFit];
    [doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    titleLabel.text = [NSString stringWithFormat:@"设置%@", title];
    titleLabel.textColor = [UIColor colorWithRed:47 / 256.0 green:198 / 256.0 blue:197 / 256.0 alpha:1.0];
    titleLabel.font = [UIFont systemFontOfSize:14];

    //判断编辑什么类型
    if (_editStyle == WeightStyle || _editStyle == HeightStyle || _editStyle == ProvinceStyle)
    {
        UIPickerView *pickerView = [[UIPickerView alloc] init];
        
        pickerView.backgroundColor  = [UIColor whiteColor];
        
        pickerView.dataSource = self;
        
        pickerView.delegate = self;
        
        [pickerView selectRow:0 inComponent:0 animated:NO];
        
        self.pickerView = pickerView;
        
        [self addSubview:pickerView];
    }
    else
    {
        UITextField *editTextField = [[UITextField alloc] init];
        
        editTextField.text = text;
        
        editTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        editTextField.layer.borderWidth = 2;
        
        editTextField.textAlignment = NSTextAlignmentCenter;
        
        self.editTextField = editTextField;
        
        [self addSubview:editTextField];
    }
}


-(void)autoLayoutSubviewsWithView:(UIView *)inputView
{
    __weak typeof(self) weakSelf = self;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(weakSelf.containerView).offset(2);
        make.centerX.equalTo(weakSelf.containerView);
    }];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.bottom.equalTo(weakSelf.containerView).offset(-2);
        make.left.equalTo(weakSelf.containerView);
        make.right.equalTo(weakSelf.doneButton.mas_left);
    }];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.bottom.equalTo(weakSelf.containerView).offset(-2);
        make.left.equalTo(weakSelf.cancelButton.mas_right);
        make.right.equalTo(weakSelf.containerView);
        make.width.equalTo(weakSelf.cancelButton);
    }];
    
    if (inputView == self.pickerView)
    {
        [inputView mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.left.equalTo(weakSelf.containerView);
             make.right.equalTo(weakSelf.containerView);
             make.top.equalTo(weakSelf.titleLabel.mas_bottom);
             make.bottom.equalTo(weakSelf.cancelButton.mas_top);
         }];
    }
    else
    {
        [inputView mas_makeConstraints:^(MASConstraintMaker *make)
        {
            make.center.equalTo(weakSelf.containerView);
            make.height.mas_equalTo(30);
            make.width.equalTo(weakSelf.containerView).offset(-2);
        }];
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = kScreenWidth / 2;
    
    CGFloat x = (kScreenWidth - width) / 2;
    
    CGFloat height = 0;
    
    CGFloat y = 0;
    
    if (self.editStyle == UserNameStyle || self.editStyle == idCardStyle)
    {
        height = kScreenHeight / 4;
        
        y = (kScreenHeight - height) / 2;
        
        self.containerView.frame = CGRectMake(x, y, width, height);
        
        [self autoLayoutSubviewsWithView:self.editTextField];
    }
    else
    {
        if (self.editStyle == ProvinceStyle)
        {
            height = kScreenHeight / 2;
        }
        else
        {
            height = kScreenHeight / 3;
        }
        y = (kScreenHeight - height) / 2;
        
        self.containerView.frame = CGRectMake(x, y, width, height);
        
        [self autoLayoutSubviewsWithView:self.pickerView];
    }
}

#pragma mark - 点击

-(void)cancelButtonClick
{
    [self removeFromSuperview];
}
-(void)doneButtonClick
{
    if (self.editStyle == HeightStyle)
    {
        self.userInfo.height = @(self.figure);
    }
    if (self.editStyle == WeightStyle)
    {
        self.userInfo.weight = @(self.figure);
    }
    if (self.editStyle == ProvinceStyle)
    {
        self.userInfo.province = self.selectedProvince;
    }
    if (self.editStyle == idCardStyle)
    {
        self.userInfo.idCard = self.editTextField.text;
    }
    if (self.editStyle == UserNameStyle)
    {
        self.userInfo.userName = self.editTextField.text;
    }
    
    [kCoreDataManager save];
    
    if (self.doneBlock)
    {
        self.doneBlock();
    }
    
    [self removeFromSuperview];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}

#pragma mark - UIPickerViewDataSource/UIPickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.editStyle == ProvinceStyle ? 1 : 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.editStyle == ProvinceStyle)
    {
        return self.provinceArr.count;
    }
    else
    {
        if (component == 0)
        {
            return 3;
        }
        else
        {
            return 10;
        }
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (self.editStyle == ProvinceStyle)
    {
        return self.provinceArr[row];
    }
    else
    {
        return [NSString stringWithFormat:@"%zd", row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.editStyle == ProvinceStyle)
    {
        self.selectedProvince =  self.provinceArr[row];
    }
    else
    {
        if (component == 0)     self.hundred = row;
        if (component == 1)     self.ten = row;
        if (component == 2)     self.one = row;
        
        self.figure = self.hundred * 100 + self.ten * 10 + self.one;
    }
    NSLog(@"%ld", self.figure);
}

@end
